var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/imp/cpts/SourceHelp.tsx
var SourceHelp_exports = {};
__export(SourceHelp_exports, {
  SourceHelp: () => SourceHelp
});
module.exports = __toCommonJS(SourceHelp_exports);
var import_react = require("react");
var import_functionalComponent = require("../../../functionalComponent");
var import_util = require("../../../util");
var import_service = require("../service");
var import_utils = require("../utils");
var import_MasterSlaveTable = __toESM(require("./components/MasterSlaveTable"));
var import_ShuttleTable = __toESM(require("./components/ShuttleTable"));
var import_ShuttleTableHorizontal = __toESM(require("./components/ShuttleTableHorizontal"));
var import_SourceTable = __toESM(require("./components/SourceTable"));
var import_SourceTreeTable = __toESM(require("./components/SourceTreeTable"));
var import_TreeTable = __toESM(require("./components/TreeTable"));
function SourceHelp({
  sourceTree,
  sourceCount,
  busData,
  flowCode,
  defaultSourceCfg,
  detailTables,
  defaultSelectedKeys = []
}) {
  const [ins] = (0, import_functionalComponent.useModal)();
  const [getRef] = (0, import_util.useRefs)();
  const [popupMode, setPopupMode] = (0, import_react.useState)(defaultSourceCfg.popupMode);
  const [state, setState] = (0, import_util.useRefState)({
    sourceId: defaultSourceCfg.sourceId,
    columns: defaultSourceCfg.columns || [],
    queryItems: defaultSourceCfg.queryItems || [],
    allowMultiSelect: defaultSourceCfg.allowMultiSelect || 0,
    loading: false,
    subTableTitle: defaultSourceCfg.subTableTitle || "",
    masterQueryItems: defaultSourceCfg.masterQueryItems || [],
    masterColumns: defaultSourceCfg.masterColumns || [],
    masterFormId: defaultSourceCfg.masterFormId || "",
    treeFieldConfig: defaultSourceCfg.treeFieldConfig,
    treeField: defaultSourceCfg.masterFormId || "",
    autoFilterRef: defaultSourceCfg.masterFormId || 0
  });
  const [selectedMainRows, setSelectedMainRows] = (0, import_react.useState)([]);
  const [orgTreeState, setOrgTreeState] = (0, import_react.useState)();
  const getSourceId = (sId) => {
    const sourceId = sId || state.sourceId;
    return (sourceId == null ? void 0 : sourceId.indexOf("_")) ? import_util.iup.split(sourceId, "_")[1] : sourceId;
  };
  const refreshConfig = (0, import_react.useCallback)(async ({ sourceId: sId }) => {
    const sourceId = getSourceId(sId);
    const {
      columns = [],
      queryItems = [],
      allowMultiSelect,
      popupMode: popupMode2,
      masterQueryItems,
      masterColumns,
      masterFormId,
      treeFieldConfig,
      treeField,
      popupName,
      autoFilterRef
    } = await (0, import_service.loadSourceConfig)({ sourceId });
    setState({
      loading: false,
      sourceId: sId,
      queryItems,
      columns,
      allowMultiSelect,
      masterQueryItems,
      masterColumns,
      masterFormId,
      treeFieldConfig,
      treeField,
      subTableTitle: popupName,
      autoFilterRef
    });
    setPopupMode(popupMode2);
    setSelectedMainRows([]);
    setOrgTreeState("");
  }, []);
  const onSelect = (keys) => {
    refreshConfig({ sourceId: keys[0] });
  };
  ins.setApi({
    async getResult() {
      let ds = getRef("table").current.getApi().getSelectedData() || [];
      let mainRuleHelpInfo;
      if (state.allowMultiSelect === 1 && [4, 5].includes(popupMode)) {
        ds = (getRef("table").current.getApi().getData() || []).filter((row) => row.bizFlowRefed !== 1);
      }
      if (popupMode === 3 && state.allowMultiSelect === 2) {
        mainRuleHelpInfo = {
          sourceDatas: getRef("tableMain").current.getApi().getSelectedData() || [],
          id: state.masterFormId,
          flowCode
        };
      }
      if (ds == null ? void 0 : ds.length) {
        const [success, data] = await (0, import_service.converSourceData)({
          sourceId: getSourceId(),
          flowCode,
          sourceDatas: ds,
          detailTables,
          mainRuleHelpInfo
        });
        if (success) {
          return data || {};
        }
        import_util.iup.alert(data);
      } else {
        import_util.iup.alert("请先选择数据行！");
      }
      return null;
    }
  });
  const renderTree = () => {
    if (sourceCount < 2) {
      return false;
    }
    return /* @__PURE__ */ React.createElement(import_util.Layout.Slider, { size: 200, style: { marginRight: 8 } }, /* @__PURE__ */ React.createElement(
      "div",
      {
        hidden: sourceCount < 2,
        style: { border: import_util.cssVar.border, borderRadius: 4, height: "100%", overflow: "auto" }
      },
      /* @__PURE__ */ React.createElement(import_functionalComponent.Tree, { treeData: sourceTree, defaultExpandAll: true, defaultSelectedKeys: [state.sourceId], onSelect })
    ));
  };
  const onDataLoad = (0, import_util.useRefCallback)((table) => {
    if ((!popupMode || popupMode === 1) && !state.autoFilterRef) {
      const { dataSource = [] } = table.getState() || {};
      const selectIndex = (0, import_utils.findFilterIndexes)(dataSource, (it) => defaultSelectedKeys.includes(it.id));
      table.setSelected(selectIndex);
    }
  });
  const renderTable = () => {
    const globalParams = {
      state,
      popupMode,
      flowCode,
      busData,
      getRef,
      onDataLoad,
      defaultSelectedKeys,
      rowDisable: (row) => row.bizFlowRefed === 1,
      iconVisible: state.autoFilterRef === 1
    };
    if (popupMode === 5 && state.allowMultiSelect === 1) {
      return /* @__PURE__ */ React.createElement(import_ShuttleTableHorizontal.default, { ...globalParams });
    }
    if (popupMode === 4 && state.allowMultiSelect === 1) {
      return /* @__PURE__ */ React.createElement(import_ShuttleTable.default, { ...globalParams });
    }
    if (popupMode === 3) {
      return /* @__PURE__ */ React.createElement(import_MasterSlaveTable.default, { ...globalParams, dependentState: selectedMainRows, changeDependent: setSelectedMainRows });
    }
    if (popupMode === 2) {
      return /* @__PURE__ */ React.createElement(import_TreeTable.default, { ...globalParams, dependentState: orgTreeState, changeDependent: setOrgTreeState });
    }
    if (popupMode == 1) {
      return /* @__PURE__ */ React.createElement(import_SourceTreeTable.default, { ...globalParams });
    }
    return /* @__PURE__ */ React.createElement(import_SourceTable.default, { ...globalParams });
  };
  return /* @__PURE__ */ React.createElement(import_util.Layout, { direction: "row", autoFit: true, loading: state.loading, style: { padding: 5 } }, renderTree(), /* @__PURE__ */ React.createElement(import_util.Layout.Flex, { direction: "column" }, /* @__PURE__ */ React.createElement(import_util.Layout.Flex, { style: { border: import_util.cssVar.border, borderRadius: 4 } }, renderTable())));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SourceHelp
});
